package hu.blok1.veilingsite.client.gui;

import java.util.ArrayList;
import java.util.List;

import hu.blok1.veilingsite.client.Veilingsite;
import hu.blok1.veilingsite.client.data.AccountServiceAdmin;
import hu.blok1.veilingsite.client.data.AccountServiceAdminAsync;
import hu.blok1.veilingsite.client.data.HomeService;
import hu.blok1.veilingsite.client.data.HomeServiceAsync;
import hu.blok1.veilingsite.client.data.ProductService;
import hu.blok1.veilingsite.client.data.ProductServiceAsync;
import hu.blok1.veilingsite.shared.Product;
import hu.blok1.veilingsite.shared.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class searchResults {
	private VerticalPanel vp;
	private VerticalPanel vpContent = new VerticalPanel();
	private ScrollPanel scroll = new ScrollPanel();
	private User user;
	private Veilingsite veilingsite;

	private List<Product> productList;
	private CellTable<Product> table;

	private FlexTable productFormTop = new FlexTable();

	private Button redraw = new Button();
	
	private DateTimeFormat dtf = DateTimeFormat.getFormat("dd-MM-yyyy HH:mm:ss");


	private int userID;

	private final HomeServiceAsync HomeService = GWT.create(HomeService.class);

	public searchResults(VerticalPanel a, Veilingsite vs, final String column,
			final String value) {
		veilingsite = vs;
		user = veilingsite.getUser();
		userID = user.getId();
		veilingsite.reloadHeader();

		vp = a;
		vp.clear();

		Label pageTitle = new Label("Huidige zoekopdracht: " + value);



		redraw.setText("Vernieuw");

		productFormTop.setCellSpacing(6);

		
		productFormTop.setWidget(1, 0, new Label(
				"Productoverzicht vernieuwen: "));
		productFormTop.setWidget(1, 1, redraw);

		vpContent.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		vpContent.add(pageTitle);
		
		// start search box
		HorizontalPanel horisearchpanel = new HorizontalPanel();
		final TextBox searchtb = new TextBox();
	    searchtb.setWidth("450px");
	    Button searchbutton = new Button("Start zoekopdracht!");
	    searchbutton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem("searchResults//SEARCH//"+ searchtb.getText());
			}
		});
	    horisearchpanel.add(searchtb);
	    horisearchpanel.add(searchbutton);
		vpContent.add(horisearchpanel);
		// end searchbox
		vpContent.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		vpContent.add(productFormTop);
		vpContent.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		getProductData(column, value);

		redraw.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				getProductData(column, value);
			}
		});

	

	}

	private void getProductData(String column, String value) {
		productList = null;
		table = null;
		productList = new ArrayList<Product>();
		table = new CellTable<Product>();
		table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		

		createCellPanel();

		HomeService.GetProducts(column, value,
				new AsyncCallback<List<Product>>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Product(en) opvragen is mislukt");
					}

					@Override
					public void onSuccess(List<Product> result) {
						if (result.isEmpty()) {
							Window.alert("Er zijn helaas geen producten gevonden");
						} else {
							table.setRowData(result);
						}
					}
				});
		scroll.clear();
		scroll.setHeight("30em");
		scroll.add(table);
		vpContent.setWidth("100%");
		vpContent.add(scroll);
		vp.add(vpContent);
	}

	private void createCellPanel() {
		table.setWidth("94%", true);
		table.setTableLayoutFixed(true);

		// Add a text column to show the name. // 2
		TextColumn<Product> nameColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return object.getName();
			}
		};
		// nameColumn.setSortable(true);
		table.addColumn(nameColumn, "Titel");
		table.setColumnWidth(nameColumn, "20%");

		// Add a text column to show the minimumPrice. // 3
		TextColumn<Product> minimumPriceColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
//				return String.valueOf(object.getMinimumPrize());
				return String.valueOf(object.getHighestBid());
			}
		};
		minimumPriceColumn.setSortable(true);
		table.addColumn(minimumPriceColumn, "Huidige prijs");
		table.setColumnWidth(minimumPriceColumn, "8%");

		// Add a text column to show the endDate. // 5
		TextColumn<Product> endTimestampColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(dtf.format(object.getEndDate()));
			}
		};
		// startTimestampColumn.setSortable(true);
		table.addColumn(endTimestampColumn, "Einddatum");
		table.setColumnWidth(endTimestampColumn, "15%");

		// Add a text column to show the description. // 7
		TextColumn<Product> descriptionColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				if (object.getDescription().length() > 80) {
					return String.valueOf(object.getDescription().substring(0,
							82)+ "...");
				} else {
					return String.valueOf(object.getDescription());
				}
			}
		};
		table.addColumn(descriptionColumn, "Beschrijving");
		table.setColumnWidth(descriptionColumn, "35%");

		// Add a text column to show the category. // 10
		TextColumn<Product> categoryColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return String.valueOf(object.getCategory());
			}
		};
		table.addColumn(categoryColumn, "Categorie");
		table.setColumnWidth(categoryColumn, "20%");
		
		// Add a selection model to handle user selection.
		final SingleSelectionModel<Product> selectionModel = new SingleSelectionModel<Product>();
		table.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						Product selected = selectionModel
								.getSelectedObject();
						if (selected != null) {
							History.newItem("productSpecifications//"+ selected.getProductID());
						}
					}
				});



		// table.getColumnSortList().push(endTimestampColumn);
		table.setRowCount(productList.size(), true);
		table.setRowData(0, productList);
	}
}